package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.itheima.reggie.dao.DishDao;
import com.itheima.reggie.dao.DishFlavorDao;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.domain.PageBean;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.exception.CustomException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements DishService {

    @Autowired
    private DishDao dishDao;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorDao dishFlavorDao;

    //添加菜品
    @Override
    public void addDish(DishDto dishDto) {

        try{
            //将菜品存入数据库产生id
            dishDao.insert(dishDto);
            //获取dishDto中的id
            Long id = dishDto.getId();
            //将id存入DishFlavor属性中
            List<DishFlavor> flavors = dishDto.getFlavors();
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);
                dishFlavorDao.insert(flavor);
            }

        }catch (Exception c){
            throw new CustomException("该菜品已存在,请重新输入");
        }

    }

    //菜品分页查询
    @Override
    public PageBean findPage(int page, int pageSize, String name) {


        //起始索引=(当前页码-1)*每页条目数
       int start = (page-1)*pageSize;
        List<Dish> dishes = dishDao.selectPage(start, pageSize, name);
        int total = dishDao.findCount(name);

        PageBean p = new PageBean();
        p.setRecords(dishes);
        p.setTotal(total);
        return p;
    }

    //菜品数据回显
    public DishDto selectById(Long id){

        Dish dish = dishDao.selectById(id);

        DishDto dishDto = new DishDto();

        //BeanUtils.copyProperties()方法的作用是将dish中的数据拷贝到dishDto中
        BeanUtils.copyProperties(dish,dishDto);

        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();

        lqw.eq(DishFlavor::getDishId,id);

        List<DishFlavor> dishFlavors = dishFlavorDao.selectList(lqw);

        dishDto.setFlavors(dishFlavors);

        return dishDto;

    }

    //菜品修改
    @Override
    public void update(DishDto dishDto) {

        dishDao.updateById(dishDto);

        //先将这个菜品对应的口味都删除,再添加新的
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dishDto.getId());
        //delete from flavor where dish_id = id
        dishFlavorDao.delete(lqw);

        List<DishFlavor> flavors = dishDto.getFlavors();

        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
            dishFlavorDao.insert(flavor);
        }



    }

    //修改后的菜品列表
    @Override
    public List<DishDto> findByCategoryId(Dish dish) {

        //select * from dish where category_id = ? and status = ? order by sort asc,update_time desc
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        //lqw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        lqw.eq(Dish::getStatus,1);

        List<Dish> dishes = dishDao.selectList(lqw);

        List<DishDto> dishess = new ArrayList<>();


        //将dishes中的数据拷贝到dishess中

            for (Dish dish1 : dishes) {
                DishDto dishDto = new DishDto();
                Long categoryId = dish1.getCategoryId();
                Category category = categoryService.getById(categoryId);
                if (category!=null){
                    dish1.setCategoryName(category.getName());
                }
                BeanUtils.copyProperties(dish1,dishDto);
                dishess.add(dishDto);
            }




        for (DishDto dishDto1 : dishess) {
            Long id = dishDto1.getId();
            LambdaQueryWrapper<DishFlavor> lqw1  = new LambdaQueryWrapper<>();
            //select * from dish_flavor where dishid = id
            lqw1.eq(DishFlavor::getDishId,id);
            List<DishFlavor> dishFlavors = dishFlavorDao.selectList(lqw1);

            dishDto1.setFlavors(dishFlavors);

        }


        return dishess;
    }


    //单个停售与批量停售
    @Override
    public void getStatus(int status, Long[] ids) {

        //根据id将菜品的status修改如果是1改成0,是0改成1

        //根据id数组查询到所有菜品对象
        List<Dish> dishes = dishDao.selectBatchIds(Arrays.asList(ids));


        //遍历所有菜品对象
        for (Dish dish : dishes) {

            UpdateWrapper<Dish> updateWrapper = new UpdateWrapper();
            updateWrapper.eq("id",dish.getId());
            updateWrapper.set("status",status);
            dishDao.update(dish,updateWrapper);

           /* if (status==0){
                    UpdateWrapper<Dish> updateWrapper = new UpdateWrapper();
                    updateWrapper.eq("id",dish.getId());
                    updateWrapper.set("status",0);
                    dishDao.update(dish,updateWrapper);
                    //update dish set status=1 where id = #{id}
            }
            else{
                UpdateWrapper<Dish> updateWrapper = new UpdateWrapper();
                updateWrapper.eq("id",dish.getId());
                updateWrapper.set("status",1);
                dishDao.update(dish,updateWrapper);
            }
        }*/


        //封装返回

    }

    /*@Override
    public void getDishStatus(int status, List<Long> ids) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ids!=null,Dish::getId,ids);
        List<Dish> dishList = dishDao.selectList(queryWrapper);
        for (Dish dish : dishList) {
            if (dish!=null){
                dish.setStatus(status);
                dishDao.updateById(dish);
            }
        }*/
    }

}
